 #!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
import os
import sys
from xml.dom import minidom


# Per pensar .... posar butons de + - , si hi ha molts perfils
# possible XML de configuracio amb els perfils i nom de les imatges
class Boto(gtk.Button):
	pass
	def __init__(self):
		gtk.Button.__init__(self)

class Buttons:

    def callback(self, widget, data=None):
        print "%s" % data
    def enter(self, widget, data=None):
	self.window.set_title(widget.Name)
	titleProfile.set_text(widget.Name)


    def __init__(self):

        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.window.set_title("LauncherXWii")
        self.window.connect("destroy", lambda wid: gtk.main_quit())
        self.window.connect("delete_event", lambda a1,a2:gtk.main_quit())
        self.window.set_border_width(2)
	maxFils = gtk.gdk.screen_height() / 150
	maxCols = gtk.gdk.screen_width() / 150
	table = gtk.Table(maxFils, maxCols,False)
	vbox = gtk.VBox(False, 0)
	vbox.pack_start(table, True, True, 2)
	table2 = gtk.Table(1, 3,True)
	vbox.pack_end(table2, True, True, 2)
	vbox.show()
	self.window.add(vbox)  
	os.chdir(sys.path[0])
	xmldoc = minidom.parse('profiles.xml')
	elements = xmldoc.getElementsByTagName('profiles')

	index_num = 1

	#print maxFils
	#print maxCols
	#print gtk.gdk.screen_width()
	#print gtk.gdk.screen_height()
	posX = 0
	posY = 1
	maxColumn = maxCols
	posItems = 1
	for x in range((maxFils -1 ) * maxCols):
		if x < len(xmldoc.getElementsByTagName('profiles')[0].getElementsByTagName('profile')):
			data = elements[0].getElementsByTagName('profile')[x].getAttribute('image')
			image = gtk.Image()
			image.set_from_pixbuf(gtk.gdk.pixbuf_new_from_file(data).scale_simple(150,150,gtk.gdk.INTERP_BILINEAR))
			image.show()

			button =  Boto()
			button.Name =  elements[0].getElementsByTagName('profile')[x].getAttribute('name')
			button.connect("clicked",self.callback, elements[0].getElementsByTagName('profile')[x].getAttribute('name'))
			button.connect("focus", self.enter)

			button.add(image)
			if index_num > maxColumn :
				posX = posX + 1
				posY = posY + 1
				maxColumn = maxColumn + maxCols
				posItems = 1
			table.attach(button,posItems-1,posItems,posX,posY)
			button.show()
			posItems =  posItems + 1
	   		index_num = index_num + 1
		else:
			data = "images/empty.jpg"
			image = gtk.Image()
			image.set_from_pixbuf(gtk.gdk.pixbuf_new_from_file(data).scale_simple(150,150,gtk.gdk.INTERP_BILINEAR))
			image.show()

			button = gtk.Button()
			button.Name = ""
			button.connect("focus", self.enter)
			button.add(image)
			if index_num > maxColumn :
				posX = posX + 1
				posY = posY + 1
				maxColumn = maxColumn + maxCols
				posItems = 1
			table.attach(button,posItems-1,posItems,posX,posY)
			button.show()
			posItems =  posItems + 1
	   		index_num = index_num + 1

	data = "images/prev.jpg"
	image = gtk.Image()
	image.set_from_file(data)
	image.show()

	button = Boto()
	button.Name = "Prev"

	button.connect("clicked",self.callback,"")			
	button.add(image)
	table2.attach(button,0,1,2,3)
	button.show()

	data = "images/next.jpg"
	image = gtk.Image()
	image.set_from_file(data)
	image.show()

	button =  Boto()
	button.Name = "Next"
	button.connect("clicked",self.callback, "")			
	button.add(image)
	table2.attach(button,2 ,3,2,3)
	button.show()	  
 	
	global titleProfile 
	titleProfile = gtk.Label("test")
	table2.attach(titleProfile,1,2,2,3)
	titleProfile.show()
	table2.show()
	table.show()
	self.window.show()


def main():
        gtk.main()
        return 0

if __name__ == "__main__":
    Buttons()
    main()
